import { _decorator, Color, Component, EventTouch, Graphics, Input, instantiate, Node } from 'cc';
import { ExportData } from '../../../Manager/PathManager/PathManager';
import { ZumaClearMajiang } from './ZumaClearMajiang';
import { GAME_PREFAB, GlobalData } from '../../../Data/GlobalData';
const { ccclass, property } = _decorator;

@ccclass('ZumaClearLoad')
/**
 * 祖玛消失层
 */
export class ZumaClearLoad extends Component {

    //点击层
    @property(Node)
    private TouchNode: Node = null;

    //路线
    @property(Graphics)
    private Line: Graphics = null;

    //麻将层
    @property(Node)
    private MahJongNode: Node = null;

    //身体距离
    public BodySpeed: number = 100;

    //麻将数组
    public MahJongList: Node[] = [];
    public NoUse: Node[] = [];

    //回退麻将数组
    public BackMahJongList: Node[] = [];

    //显示麻将数量
    public ShowMahJongNum: number = 10;

    //麻将信息
    public MahJongInfo: any = {

    }

    protected onLoad(): void {
        this.TouchNode.on(Input.EventType.TOUCH_START, this.TouchStart, this);
    }

    /**
     * 更新关卡
     */
    public UpdateLevel() {
        this.DrawLine();

        this.NoUse = [];

        //
        for (let sm of this.MahJongList) {
            sm.setParent(null);
            this.NoUse.push(sm);
        }

        //
        for (let sm of this.MahJongList) {
            sm.setParent(null);
            this.NoUse.push(sm);
        }

        this.MahJongList = [];
        this.BackMahJongList = [];

        app.pathManager.UpdatePathInfo(app.resManager.Json["level0"]);

        //创建龙头
        this.CreateMoveMahJong("dragonhead");
        this.CreateMoveMahJong("majiang_dir0");
    }
    

    protected update(dt: number): void {

        if (GlobalData.GameOver) return;

        //头部移动
        if (this.MahJongList.length > 0) {
            this.MahJongList[0].getComponent(ZumaClearMajiang).MoveSelf(dt);
        }

        //身体跟随
        for (let i = 1; i < this.MahJongList.length; i++) {
            this.MahJongList[i].getComponent(ZumaClearMajiang).UpdateDistance(this.MahJongList[i - 1].getComponent(ZumaClearMajiang).CurrentDistance - this.BodySpeed);
        }
    }

    /**
     * 绘制路线
     */
    private DrawLine() {
        let line_info: ExportData = app.resManager.Json["level0"];

        console.log(line_info)

        //绘制贝塞尔弧线


        this.Line.clear();
        let curves = line_info.curves;

        curves.forEach(curve => {
            // 开始绘制
            //绘制贝塞尔弧线
            this.Line.strokeColor = new Color(255, 0, 0, 100);
            this.Line.lineWidth = 20;
            this.Line.moveTo(curve.startPoint.x, curve.startPoint.y);
            this.Line.quadraticCurveTo(
                curve.controlPoint.x, curve.controlPoint.y,
                curve.endPoint.x, curve.endPoint.y
            );
            // 应用描边
            this.Line.stroke();
        });
    }

    /**
     * 创建移动麻将
     */
    private CreateMoveMahJong(_name: string) {

        let sm: Node = null;

        if (this.NoUse.length > 0) {
            sm = this.NoUse.shift();
        }

        sm = instantiate(app.resManager.Prefabs[GAME_PREFAB.ClearMajiang])

        sm.getComponent(ZumaClearMajiang).UpdateMajiang(_name);
        sm.setParent(this.MahJongNode);
        sm.setSiblingIndex(0);

        if (this.MahJongList.length > 0) {
            sm.getComponent(ZumaClearMajiang).UpdateDistance(this.MahJongList[this.MahJongList.length - 1].getComponent(ZumaClearMajiang).CurrentDistance - this.BodySpeed);
        } else {
            sm.getComponent(ZumaClearMajiang).UpdateDistance(0);
        }

        this.MahJongList.push(sm);

    }

    /**
     * 
     */
    private TouchStart(_e: EventTouch) {

    }
}


